package secrpc

import (
	"dub/define/grpcmodel"
	"dub/utils"
	"fmt"
	"golang.org/x/net/context"
	"time"
)

type SecRpcMem struct {
	log   *utils.Logger
	dbRpc *utils.RpcProxy
}

//查询会员信息
func (s *SecRpcMem) Find(ctx context.Context, args *grpcmodel.ModelSecRpcMemReqFind) (reply *grpcmodel.ModelSecRpcMemResFind, err error) {
	reply = &grpcmodel.ModelSecRpcMemResFind{}
	for {
		f_args := &grpcmodel.ModelDbRpcMemReqFind{
			IsPage:   args.IsPage,
			StartRow: args.StartRow,
			PageSize: args.PageSize,
			AddUseId: args.AddUseId,
		}
		db_mem := grpcmodel.NewIDbRpcMemClient(s.dbRpc.Conn())
		s.log.Infof("def_func_sec_mem.go Find method call db_mem.Find f_args info is %+v\n", f_args)
		f_reply, err := db_mem.Find(s.dbRpc.Context(), f_args)
		s.log.Infof("def_func_sec_mem.go Find method call db_mem.Find f_reply info is %+v\n", f_reply)
		if err != nil {
			s.log.Errorf("def_func_sec_mem.go Find method call db_mem.Find err. %v\n", err)
			reply.Err = 2
			break
		}
		if f_reply.GetErr() > 0 {
			if f_reply.GetErr() > 1 {
				s.log.Errorf("def_func_sec_mem.go Find method call db_mem.Find err. %v\n", err)
			} else {
				s.log.Errorf("def_func_sec_mem.go Find method call db_mem.Find no find info. %v\n", err)
			}
			reply.Err = f_reply.GetErr()
			break
		}

		reply.Total = f_reply.GetTotal()
		res_models := make([]*grpcmodel.ModelSecRpcMem, 0, len(f_reply.GetRows()))
		for _, mem := range f_reply.Rows {
			res_model := &grpcmodel.ModelSecRpcMem{
				MemId:        mem.GetMemId(),
				MemName:      mem.GetMemName(),
				MemPhone:     mem.GetMemPhone(),
				MemBirthDate: mem.GetMemBirthDate(),
				MemSex:       mem.GetMemSex(),
				MemHeight:    mem.GetMemHeight(),
				MemEducation: mem.GetMemEducation(),
				MemIncome:    mem.GetMemIncome(),
				MemJob:       mem.GetMemJob(),
				MemHouse:     mem.GetMemHouse(),
				MemCar:       mem.GetMemCar(),
				MemStatus:    mem.GetMemStatus(),
				MemAddTime:   mem.GetMemAddTime(),

				//用于显示的字段
				ShowAddTime:   time.Unix(int64(mem.GetMemAddTime()), 0).Format("2006-01-02"),
				ShowBirthDate: time.Unix(int64(mem.GetMemBirthDate()), 0).Format("2006-01-02"),
			}

			res_models = append(res_models, res_model)
		}
		reply.Rows = res_models
		break
	}
	return
}

//修改会员信息
func (s *SecRpcMem) Update(ctx context.Context, args *grpcmodel.ModelSecRpcMemReqUpdate) (reply *grpcmodel.ModelSecRpcMemResUpdate, err error) {
	reply = &grpcmodel.ModelSecRpcMemResUpdate{}
	for {
		//把修改的会员查询出来
		f_args := &grpcmodel.ModelDbRpcMemReqFind{MemId: args.ModelSecRpcMem.GetMemId()}
		db_mem := grpcmodel.NewIDbRpcMemClient(s.dbRpc.Conn())
		s.log.Infof("def_func_sec_mem.go Update method call db_mem.FindMem f_args info is %+v\n", f_args)
		f_reply, err := db_mem.FindMem(s.dbRpc.Context(), f_args)
		s.log.Infof("def_func_sec_mem.go Update method call db_mem.FindMem f_reply info is %+v\n", f_reply)
		if err != nil {
			s.log.Errorf("def_func_sec_mem.go Update method db_mem.FindMem err. %v\n", err)
			reply.Err = 2
			break
		}
		if f_reply.GetErr() > 0 {
			if f_reply.GetErr() > 1 {
				s.log.Errorf("def_func_sec_mem.go Update method call db_mem.FindMem err. %v\n", err)
			} else {
				s.log.Errorf("def_func_sec_mem.go Update method no find error err. %v\n", err)
			}
			reply.Err = f_reply.GetErr()
			break
		}

		u_args := &grpcmodel.ModelDbRpcMemReqModel{}
		u_args.ModelDbRpcMem = f_reply.GetModelDbRpcMem()

		//设置修改的信息
		switch args.GetUpdateType() {
		case 1: //图片追加
			if u_args.GetModelDbRpcMem().GetMemShowPics() != "" {
				u_args.GetModelDbRpcMem().MemShowPics = fmt.Sprintf("%s,%s", u_args.GetModelDbRpcMem().GetMemShowPics(), args.GetModelSecRpcMem().GetMemShowPics())
			} else {
				u_args.GetModelDbRpcMem().MemShowPics = args.GetModelSecRpcMem().GetMemShowPics()
			}
		default:
			if args.GetModelSecRpcMem().GetMemName() != "" {
				u_args.GetModelDbRpcMem().MemName = args.GetModelSecRpcMem().GetMemName()
			}
			if args.GetModelSecRpcMem().MemPhone != "" {
				u_args.GetModelDbRpcMem().MemPhone = args.GetModelSecRpcMem().GetMemPhone()
			}
			if args.GetModelSecRpcMem().GetMemBirthDate() > 0 {
				u_args.GetModelDbRpcMem().MemBirthDate = args.GetModelSecRpcMem().GetMemBirthDate()
			}
			if args.GetModelSecRpcMem().GetMemWxCode() != "" {
				u_args.GetModelDbRpcMem().MemWxCode = args.GetModelSecRpcMem().GetMemWxCode()
			}
			if args.GetModelSecRpcMem().GetMemQqCode() != "" {
				u_args.GetModelDbRpcMem().MemQqCode = args.GetModelSecRpcMem().GetMemQqCode()
			}
			if args.GetModelSecRpcMem().GetMemSex() != "" {
				u_args.GetModelDbRpcMem().MemSex = args.GetModelSecRpcMem().GetMemSex()
			}
			if args.GetModelSecRpcMem().GetMemHeight() > 0 {
				u_args.GetModelDbRpcMem().MemHeight = args.GetModelSecRpcMem().GetMemHeight()
			}
			if args.GetModelSecRpcMem().GetMemEducation() != "" {
				u_args.GetModelDbRpcMem().MemEducation = args.GetModelSecRpcMem().GetMemEducation()
			}
			if args.GetModelSecRpcMem().GetMemLiveRegionCode() != "" {
				u_args.GetModelDbRpcMem().MemLiveRegionCode = args.GetModelSecRpcMem().GetMemLiveRegionCode()
			}
			if args.GetModelSecRpcMem().GetMemCensusRegionCode() != "" {
				u_args.GetModelDbRpcMem().MemCensusRegionCode = args.GetModelSecRpcMem().GetMemCensusRegionCode()
			}
			if args.GetModelSecRpcMem().GetMemIncome() > 0 {
				u_args.GetModelDbRpcMem().MemIncome = args.GetModelSecRpcMem().GetMemIncome()
			}
			if args.GetModelSecRpcMem().GetMemJob() != "" {
				u_args.GetModelDbRpcMem().MemJob = args.GetModelSecRpcMem().GetMemJob()
			}
			if args.GetModelSecRpcMem().GetMemShowPics() != "" {
				u_args.GetModelDbRpcMem().MemShowPics = args.GetModelSecRpcMem().GetMemShowPics()
			}
			if args.GetModelSecRpcMem().GetMemHouse() != "" {
				u_args.GetModelDbRpcMem().MemHouse = args.GetModelSecRpcMem().GetMemHouse()
			}
			if args.GetModelSecRpcMem().GetMemCar() != "" {
				u_args.GetModelDbRpcMem().MemCar = args.GetModelSecRpcMem().GetMemCar()
			}
			if args.GetModelSecRpcMem().GetMemLabelIds() != "" {
				u_args.GetModelDbRpcMem().MemLabelIds = args.GetModelSecRpcMem().GetMemLabelIds()
			}
			if args.GetModelSecRpcMem().GetMemRequirement() != "" {
				u_args.GetModelDbRpcMem().MemRequirement = args.GetModelSecRpcMem().GetMemRequirement()
			}
			if args.GetModelSecRpcMem().GetMemEvaluation() != "" {
				u_args.GetModelDbRpcMem().MemEvaluation = args.GetModelSecRpcMem().GetMemEvaluation()
			}
		}

		s.log.Infof("def_func_sec_mem.go Update method db_mem.Update u_args info is %+v\n", u_args)
		u_reply, err := db_mem.Update(s.dbRpc.Context(), u_args)
		s.log.Infof("def_func_sec_mem.go Update method db_mem.Update u_reply info is %+v\n", u_reply)
		if err != nil {
			s.log.Errorf("def_func_sec_mem.go Update method db_mem.Update err. %v\n", err)
			reply.Err = 2
			break
		}
		if u_reply.GetErr() > 0 {
			if u_reply.GetErr() > 1 {
				s.log.Errorf("def_func_sec_mem.go Update method db_mem.Update err. %v\n", err)
			}
			reply.Err = u_reply.GetErr()
			break
		}
		reply.MemId = args.GetModelSecRpcMem().GetMemId()

		break
	}
	return
}

func (s *SecRpcMem) FindMem(ctx context.Context, args *grpcmodel.ModelSecRpcMemReqFind) (reply *grpcmodel.ModelSecRpcMemResFindMem, err error) {
	reply = &grpcmodel.ModelSecRpcMemResFindMem{}

	db_args := &grpcmodel.ModelDbRpcMemReqFind{}
	if args.GetMemId() > 0 {
		db_args.MemId = args.MemId
	}
	db_mem := grpcmodel.NewIDbRpcMemClient(s.dbRpc.Conn())
	s.log.Infof("def_func_sec_mem.go FindMem method db_mem.FindMem db_args info is %+v\n", db_args)
	db_reply, err := db_mem.FindMem(s.dbRpc.Context(), db_args)
	s.log.Infof("def_func_sec_mem.go FindMem method db_mem.FindMem db_reply info is %+v\n", db_reply)
	for {
		if err != nil {
			s.log.Errorf("def_func_sec_mem.go FindMem method s.dbRpc.Call MemRpc.FindMem err. %v\n", err)
			reply.Err = 2
			break
		}

		if db_reply.GetErr() > 0 {
			if db_reply.GetErr() > 1 {
				s.log.Errorf("def_func_sec_mem.go FindMem method s.dbRpc.Call MemRpc.FindMem err. %v\n", err)
			}
			reply.Err = db_reply.GetErr()
			break
		}

		re_model := &grpcmodel.ModelSecRpcMem{}
		db_model := db_reply.GetModelDbRpcMem()

		re_model.MemId = db_model.GetMemId()
		re_model.MemName = db_model.GetMemName()
		re_model.MemPhone = db_model.GetMemPhone()
		re_model.MemBirthDate = db_model.GetMemBirthDate()
		re_model.MemWxCode = db_model.GetMemWxCode()
		re_model.MemQqCode = db_model.GetMemQqCode()
		re_model.MemSex = db_model.GetMemSex()
		re_model.MemHeight = db_model.GetMemHeight()
		re_model.MemEducation = db_model.GetMemEducation()
		re_model.MemLiveRegionCode = db_model.GetMemLiveRegionCode()
		re_model.MemCensusRegionCode = db_model.GetMemCensusRegionCode()
		re_model.MemIncome = db_model.GetMemIncome()
		re_model.MemJob = db_model.GetMemJob()
		re_model.MemShowPics = db_model.GetMemShowPics()
		re_model.MemHouse = db_model.GetMemHouse()
		re_model.MemCar = db_model.GetMemCar()
		re_model.MemLabelIds = db_model.GetMemLabelIds()
		re_model.MemRequirement = db_model.GetMemRequirement()
		re_model.MemEvaluation = db_model.GetMemEvaluation()
		re_model.MemStatus = db_model.GetMemStatus()
		re_model.MemAddType = db_model.GetMemAddType()
		re_model.MemAddUseId = db_model.GetMemAddUseId()
		re_model.MemAddTime = db_model.GetMemAddTime()
		re_model.MemEditTime = db_model.GetMemEditTime()

		//显示字段的信息
		if args.GetIsFindShow() {
			re_model.ShowAddTime = time.Unix(int64(db_model.GetMemAddTime()), 0).Format("2006-01-02")
			re_model.ShowBirthDate = time.Unix(int64(db_model.GetMemBirthDate()), 0).Format("2006-01-02")
			labelMap := d_secRpcMemLabel.labelMaps()
			regionMap := _secRpcRegion.regionMaps()
			re_model.ShowLiveRegionCode = _secRpcRegion.allRegionName(regionMap, re_model.MemLiveRegionCode)
			re_model.ShowCensusRegionCode = _secRpcRegion.allRegionName(regionMap, re_model.MemCensusRegionCode)
			re_model.ShowLabelIds = d_secRpcMemLabel.labelIds2NameStrings(re_model.MemLabelIds, labelMap)
		}

		reply.ModelSecRpcMem = re_model

		break
	}
	return
}

func (s *SecRpcMem) Add(ctx context.Context, args *grpcmodel.ModelSecRpcMemReqUpdate) (reply *grpcmodel.ModelSecRpcMemResUpdate, err error) {
	reply = &grpcmodel.ModelSecRpcMemResUpdate{}
	//判断是否有信息重复
	re_args := &grpcmodel.ModelDbRpcMemReqFind{
		IsRepeat:  true,
		MemPhone:  args.GetModelSecRpcMem().GetMemPhone(),
		MemQqCode: args.GetModelSecRpcMem().GetMemQqCode(),
		MemWxCode: args.GetModelSecRpcMem().GetMemWxCode(),
	}
	db_mem := grpcmodel.NewIDbRpcMemClient(s.dbRpc.Conn())
	s.log.Infof("def_func_sec_mem.go Add method db_mem.FindMem re_args info is %+v\n", re_args)
	re_reply, err := db_mem.FindMem(s.dbRpc.Context(), re_args)
	s.log.Infof("def_func_sec_mem.go Add method db_mem.FindMem re_reply info is %+v\n", re_reply)
	for {
		if err != nil {
			s.log.Errorf("def_func_sec_mem.go Add method db_mem.FindMem err. %v\n", err)
			reply.Err = 2
			break
		}
		if re_reply.GetRepeat() {
			reply.Err = 1
			break
		}

		//添加信息
		arg_model := args.GetModelSecRpcMem()
		ad_args := &grpcmodel.ModelDbRpcMemReqModel{
			ModelDbRpcMem: &grpcmodel.ModelDbRpcMem{
				MemName:             arg_model.GetMemName(),
				MemPhone:            arg_model.GetMemPhone(),
				MemWxCode:           arg_model.GetMemWxCode(),
				MemQqCode:           arg_model.GetMemQqCode(),
				MemSex:              arg_model.GetMemSex(),
				MemEducation:        arg_model.GetMemEducation(),
				MemLiveRegionCode:   arg_model.GetMemLiveRegionCode(),
				MemCensusRegionCode: arg_model.GetMemCensusRegionCode(),
				MemJob:              arg_model.GetMemJob(),
				MemShowPics:         arg_model.GetMemShowPics(),
				MemHouse:            arg_model.GetMemHouse(),
				MemCar:              arg_model.GetMemCar(),
				MemLabelIds:         arg_model.GetMemLabelIds(),
				MemRequirement:      arg_model.GetMemRequirement(),
				MemEvaluation:       arg_model.GetMemEvaluation(),
				MemBirthDate:        arg_model.GetMemBirthDate(),
				MemHeight:           arg_model.GetMemHeight(),
				MemIncome:           arg_model.GetMemIncome(),
				MemStatus:           arg_model.GetMemStatus(),
				MemAddType:          arg_model.GetMemAddType(),
				MemAddUseId:         arg_model.GetMemAddUseId(),
			},
		}
		s.log.Infof("def_func_sec_mem.go Add method db_mem.Add ad_args info is %+v\n", ad_args)
		ad_reply, err := db_mem.Add(s.dbRpc.Context(), ad_args)
		s.log.Infof("def_func_sec_mem.go Add method db_mem.Add ad_reply info is %+v\n", ad_reply)
		if err != nil {
			s.log.Errorf("def_func_sec_mem.go Add method db_mem.Add err. %v\n", err)
			reply.Err = 2
			break
		}
		if ad_reply.GetErr() > 0 {
			reply.Err = ad_reply.GetErr()
			break
		}
		reply.MemId = ad_reply.GetMemId()
		break
	}
	return
}

var d_secRpcMem *SecRpcMem

func NewSecRpcMem(log *utils.Logger, dbRpc *utils.RpcProxy) *SecRpcMem {
	if d_secRpcMem == nil {
		d_secRpcMem = &SecRpcMem{
			log:   log,
			dbRpc: dbRpc,
		}
	}
	return d_secRpcMem
}
